import 'package:flutter/material.dart';
import 'package:hairen_app/pages/plug/func.dart';
import 'package:hairen_app/server/server.dart';
import 'package:hairen_app/server/saveInfo.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_plugin/flutter_plugin.dart';
import 'login.dart';
import 'dart:async';
typedef Widget WidgetBuilder(Map data);
class CommonPage extends StatefulWidget {
  final Widget design;
  final Future future;
  CommonPage({this.design,@required this.child,this.future}) : super();

  final WidgetBuilder child;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _CommonPageState();
  }
}

class _CommonPageState extends State<CommonPage> {
  Map userInfo = {};
  String token = "";
  List favoritesList = [];

  getInfo() {
    SaveInfo.getUserToken().then((Map ret) {
      setState(() {
        userInfo = ret['userInfo'];
        token = ret['token'];
      });
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getInfo();
  }



  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
     return new Container(
        child: new FutureBuilder(
          future:widget.future,
          builder: (context, res) {
            if (res.hasData) {
              if(res.data['code']==10001){
                return new Align(
                  child: Container(
                    height: 150.0,
                    child: new Column(
                      children: <Widget>[
                        new Text("你还没有登陆哦~"),
                        new SizedBox(
                          height: 10.0,
                        ),
                        new Text("赶快登陆，打开新世界的大门"),
                        new SizedBox(
                          height: 10.0,
                        ),
                        new RaisedButton(
                            child: new Text(
                              '登陆',
                              style: new TextStyle(color: Colors.white),
                            ),
                            onPressed: () {
                              Navigator.of(context).push(
                                new CupertinoPageRoute(
                                    builder: (context) => new Login(design: widget.design,)),
                              );
                            })
                      ],
                    ),
                  ),
                );
              }else{
                Map rows=res.data['msg'];
                return widget.child(rows);
              }
            }else{
              return new Container(
                height: 100.0,
                child: new SpinKitWave(
                  color: Color.fromRGBO(251, 114, 153, 1.0),
                  size: 30.0,
                ),
              );
            }
          },
        ),);
  }

}

